﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmUpdateStock : Form
    {
        DatabaseConnection dbconn = new DatabaseConnection();
        DataTable tbl = new DataTable();
        int i = 0 ,Index;
        
        UpdateQty myQty = new UpdateQty();
        int qtyValue, qtyAmount, Total;
        public frmUpdateStock()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmUpdateStock_Load);
            txtTempQty.KeyPress += new KeyPressEventHandler(txtTempQty_KeyPress);
            btnSearch.Click += new EventHandler(btnSearch_Click);
            btnclear.Click += new EventHandler(btnclear_Click);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
                  switch(cmbRemarks.Text)
                  {
                     
                case "In Stock":
                dbconn.dbconnection();
                qtyValue = int.Parse(tbl.Rows[i][3].ToString());
                qtyAmount = int.Parse(txtTempQty.Text);
                Total = qtyValue + qtyAmount;             
                myQty.qty = Total.ToString();
                dbconn.UpdateQuantity(myQty, Index);
                MessageBox.Show("Update Success");
                txtTempQty.Text = "";
                dbconn.DBClose();
                break;
                case "Out Stock":                    
                dbconn.dbconnection();
                qtyValue = int.Parse(tbl.Rows[i][3].ToString());
                qtyAmount = int.Parse(txtTempQty.Text);
                Total = qtyValue - qtyAmount;
                myQty.qty = Total.ToString();
                dbconn.UpdateQuantity(myQty, Index);
                MessageBox.Show("Update Success");
                txtTempQty.Text = "";
                dbconn.DBClose();
                break;
                }
                    
    }
        void btnclear_Click(object sender, EventArgs e)
        {
            cleartextbox();
            loadStock();
        }
        void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtProductID.Text == "")
            {
                MessageBox.Show("No ProductID", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtProductID.Focus();
                return;
            }
            dbconn.dbconnection();
            dbconn.loadData("SELECT  * FROM tblstock WHERE ProductID LIKE '%" + txtProductID.Text + "%'");
            DataView dw = new DataView(dbconn.tbl);
            dw.RowFilter = string.Format("ProductID LIKE '%{0}'", txtProductID.Text);
            dataGridView1.DataSource = dw;
            if (dbconn.tbl.Rows.Count > 0)
            {
                tbl = dbconn.tbl;
                txtsupplier.Text = tbl.Rows[i][2].ToString();
                txtremaining.Text = tbl.Rows[i][3].ToString();
            }
            else
            {
                MessageBox.Show("No product was found", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                cleartextbox();
            }
            dbconn.DBClose();
        }
        void txtTempQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        void frmUpdateStock_Load(object sender, EventArgs e)
        {
            txtProductID.Focus();
            cmbRemarks.Items.Add("In Stock");
            cmbRemarks.Items.Add("Out Stock");
            txtsupplier.Text = "";
            txtremaining.Text = "";
            loadStock();
            
        }
        void cleartextbox()
        {
            txtremaining.Text = "";
            txtsupplier.Text = "";
            txtTempQty.Text = "";
            txtProductID.Text = "";
            cmbRemarks.Text = null;
        
        
        }
        void loadStock()
        {
            dbconn.dbconnection();
            dbconn.loadproduct("Select distinct ProductID,SupplierName,Quantity,StockDate from tblstock");
            dataGridView1.DataSource = dbconn.tbl;
            dbconn.DBClose();
        
        }
    
      
       
    }
}
